<template>
  <div class="login reset">
    <van-cell-group>
      <van-field v-model.trim="account" required clearable label="用户名" placeholder="请输入用户名"/>
      <van-field v-model.trim="passWord" type="password" label="密码" placeholder="请输入密码" @keydown.native.enter="userLogin" required/>
    </van-cell-group>
    <div class="tip-text">
      <div>
        <input type="checkbox" value="chose" v-model="chose">
        <span>15天内自动登录</span>
      </div>
      <span>忘记密码?</span>
    </div>
    <div class="tips">{{ltips}}</div>
    <van-button type="info" @click="userLogin">立即登录</van-button>
  </div>
</template>

<script>
  import {fetch_login} from "../api";

  export default {
    name: "login",
    // inject:['app'],
    data() {
      return {
        account: '',
        groupid: '',
        chose: true,
        ltips: '',
        login_param: {},
      }
    },
    methods: {
      userLogin() {
        const params = this.login_param = {
          account: this.account,
          groupid: this.groupid
        }
        this.doLogin(params)
      },
      doLogin(param) {
        fetch_login(param).then((res) => {
          if (res.data.code === 0) {
            const token = res.data.data.token
            let userImg = ''
            if (res.data.data.user.profilePicture && res.data.data.user.profilePicture !== null){
              userImg = res.data.data.user.profilePicture
            }else{
              userImg = require('@/assets/img/null.jpg')
            }
            sessionStorage.setItem('token', token)
            sessionStorage.setItem('LoginFlag', this.$store.state.LoginFlag)
            this.$store.state.LoginFlag = false
            // this.$store.state.showLr = false
            this.$store.commit('handleAccountName', this.account)
            this.$store.commit('handleNickName', res.data.data.user.name)
            this.$store.commit('handleUserImg', userImg)
            this.$router.push({path: '/home'})
          } else {
            this.ltips = res.data.message
          }
        })
      }
    },
    mounted() {
      var that = this;
      Event.$on('userLogin', function (params) {
        that.doLogin(params)
      });
      Event.$on('relogin', function () {
        console.log('.......relogin........')
        that.doLogin(this.login_param)
      });
    }

  }
</script>

<style scoped lang="less">
  .login {
    .tip-text {
      display: flex;
      justify-content: space-around;
      color: #2795dc;
      font-size: 14px;
      margin-top: 15px;
      cursor: pointer;
    }
  }
</style>
